package com.t.bricks.bussiness.service.ordersreturned.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.ordersreturned.OrdersReturnedDao;
import com.t.bricks.bussiness.db.entity.ordersreturned.OrdersReturned;
import com.t.bricks.bussiness.service.ordersreturned.OrdersReturnedService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.U62Util;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 订单退货'OrdersReturned表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2022年06月22日 15:25:31
 */
@Service
public class OrdersReturnedServiceImpl implements OrdersReturnedService<OrdersReturned> {

	@Autowired
	private OrdersReturnedDao<OrdersReturned> ordersReturnedDao;
	
	/**
	 * 添加退单记录
	 * @param sOrderId 订单编号
	 * @Param sMemo 退单描述
	 * @return
	 */
	@Override
	public MsgEmity addCustom(String sOrderId, String sMemo) {
		if (null == sOrderId || "".equals(sOrderId.trim())) {
			return MsgEmity.err(8001, "订单编号为空");
		}
		
		if (null != sMemo && sMemo.trim().length() > 200) {
			return MsgEmity.err(8002, "内容限制200");
		}
		
		String sCreator = ModuleUtil.currentLoginUserId();
		if ("00000000/00000001".contains(sCreator)) {
			return MsgEmity.err(8003, "只有订单所属顾客才能进行退单");
		}
		
		return ordersReturnedDao.addCustom(U62Util.getUid(), sOrderId.trim(),
				sCreator, sMemo.trim());
	}

	/**
	 * 修改状态值
	 * @param sId 记录编号
	 * @param iState 待修改状态
	 * @param iVersion 版本号
	 * @return
	 */
	@Override
	public MsgEmity changeState(String sId, Integer iState, Integer iVersion) {
		if (null == iState || iState.intValue() < 2) {
			return MsgEmity.err(8001, "状态值必须大于1");
		}

		if (iState.intValue() > 6) {
			return MsgEmity.err(8002, "状态值限定[2,6]");
		}
		
		if (null == iVersion || iVersion < 1) {
			return MsgEmity.err(8003, "版本号不正确");
		}
		
		List<String> fieldNames = Arrays.asList("sOrderId", "sCustomerId", "iVersion", "iState");
		MsgEmity me = this.getValueByFieldName(sId, fieldNames, false);
		if (!me.isSuccess()) {
			return MsgEmity.err(8004, "没有找到符合的退单记录");
		}
		
		OrdersReturned ordersReturned = me.getData();
		
		if (!iVersion.equals(ordersReturned.getiVersion())) {
			return MsgEmity.err(8005, "版本号错误,记录可能已经修改");
		}
		
		if (ordersReturned.getiState() == 6) {//1:审核中;2:不通过;3:通过;4:已退款;5:已审计;6:完结
			return MsgEmity.err(8006, "记录已经完结,不能再操作");
		}

		String sCreator = ModuleUtil.currentLoginUserId();
		
		int iOrderState = 0;//订单表状态(1:下单;2:已支付;3:已出库;4:已送达;5:已签收;6:已确认;7:退货申请;8:退货审核不通过;9:退货申请通过;10:已退款;21:已审计[确认];22:已审计[退款];91:已完结[确认];92:已完结[退款])
		switch (iState) {//1:审核中;2:不通过;3:通过;4:已退款;5:已审计;6:完结
		case 2:
			iOrderState = 8;
			break;
		case 3:
			iOrderState = 9;
			break;
		case 4:
			iOrderState = 10;
			break;
		case 5:
			iOrderState = 22;
			break;
		case 6:
			iOrderState = 92;
			break;
		default:
			break;
		}
		
		return ordersReturnedDao.changeState(sId, iState, iVersion, sCreator,
				ordersReturned.getsOrderId(), iOrderState);
	}

}
